package com.czxy.service.impl;

import com.czxy.dao.GuestDao;
import com.czxy.dao.HealthDao;
import com.czxy.domain.Guest;
import com.czxy.domain.GuestVo;
import com.czxy.domain.Health;
import com.czxy.service.GuestService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zhangmengxiang1@itcast.cn
 * @version 1.0
 * @date 2020/2/6
 */
@Service
public class GuestServiceimpl implements GuestService {

    @Resource
    private GuestDao guestDao;

    @Override
    public PageInfo<Guest> findAllGuest(GuestVo guestVo) {
        PageHelper.startPage(guestVo.getPageNum(),guestVo.getPageSize());
        List<Guest> allGuest = guestDao.findAllGuest(guestVo);
        return new PageInfo<>(allGuest);
    }

    @Override
    public List<Guest> findAllGuestBook() {
        return guestDao.findAllGuestBook();
    }

    @Override
    public void deleteGuest(Integer rid) {
        guestDao.deleteByPrimaryKey(rid);
    }



    @Override
    public Set<String> findAllGuestByAdd() {
        List<Guest> allGuest = guestDao.findAllGuest(new GuestVo());
        Set<String>  set = new HashSet<String>();
        for (Guest guest : allGuest) {
            set.add(guest.getRoomname());
        }
        return set;
    }

    @Override
    public void addGuest(Guest guest) {
        System.out.println(guest);
        List<Guest> guest1 = guestDao.findOneByName(guest.getRoomname());
        guest.setRoommoney(guest1.get(0).getRoommoney());
        guest.setRoomimg(guest1.get(0).getRoomimg());
        guestDao.insert(guest);
    }



    @Override
    public void updateGuest(Guest guest) {
        //居住人数 1
        guest.setCheckinnumber(1);
        //设置状态 待审核
        guest.setState(1);

        //时间 转换 开始时间
        Date date1 = null;
        Date date2 = null;
        DateFormat df2 = null;
        String Starttime = null;
        String endTime = null;
        try {
            Starttime= guest.getStarttime().replace("Z", " UTC");
            endTime= guest.getEndtime().replace("Z", " UTC");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            Date date = df.parse(Starttime);
            Date datee = df.parse(endTime);
            SimpleDateFormat df1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            date1 = df1.parse(date.toString());
            date2 = df1.parse(datee.toString());
            df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String format = df2.format(date1);
        String format1 = df2.format(date2);
        guest.setStarttime(format);
        guest.setEndtime(format1);
        guestDao.updateByPrimaryKeySelective(guest);
    }

    @Override
    public void changeGuest(Guest guest) {
        //设置状态 通过
        guest.setState(2);
        guestDao.updateByPrimaryKeySelective(guest);
    }



    @Override
    public PageInfo<Guest> findAllGuestWYYD(GuestVo guestVo) {
        PageHelper.startPage(guestVo.getPageNum(),guestVo.getPageSize());
        List<Guest> guestList = guestDao.findAllGuestWYYD(guestVo);
        return new PageInfo<>(guestList);
    }

    @Resource
    private HealthDao healthDao;
    @Override
    public void updateGuestByRid(Integer rid) {
        Guest guest = guestDao.selectByPrimaryKey(rid);
        guest.setState(2);
        guestDao.updateByPrimaryKeySelective(guest);
        Health health = new Health();
        health.setGuest(guest);
        health.setRid(rid);
        health.setHstate(1);
        healthDao.insert(health);
    }

    @Override
    public void canceltheorder(Integer rid) {
        Guest guest = guestDao.selectByPrimaryKey(rid);
        guest.setUid(0);
        guest.setShoumoney(0.0);
        guest.setStarttime("0000-00-00 00:00:00");
        guest.setEndtime("0000-00-00 00:00:00");
        guest.setCheckinnumber(0);
        guest.setState(0);
        guestDao.updateByPrimaryKeySelective(guest);
    }

    @Override
    public PageInfo<Guest> findAllGuestWTGSH(GuestVo guestVo) {
        PageHelper.startPage(guestVo.getPageNum(),guestVo.getPageSize());
        List<Guest> guestList = guestDao.findAllGuestWTGSH(guestVo);
        return new PageInfo<>(guestList);
    }

    @Override
    public List<Guest> findAllGuestKX() {
        List<Guest> guestList = guestDao.findAllGuestKX();
        return  guestList;
    }

    @Override
    public List<Integer> amountofbusiness() {
        List<Integer> arrayList = new ArrayList<>();
        String[] arr = {"2020-02-01","2020-02-05","2020-02-10","2020-02-15","2020-02-20","2020-02-25", "2020-02-30"};
        for (int i = 0; i < 6 ; i++) {
            //arr[i]
            //arr[i+1];
            List<Guest> guestList = guestDao.amountofbusiness(arr[i] ,arr[i+1]);
            int shoumoney = 0;
            for (Guest guest : guestList) {
                shoumoney += guest.getShoumoney();
            }
                arrayList.add(shoumoney);
        }
        return arrayList;
    }


}
